<template>
  <view class="container">
    <view class="saoma_btn" @click="handleScanRental"> 点击扫码 </view>

    <div class="contont">

      <u-form labelPosition="left" :model="model" ref="uForm">

        <div v-if="device_code" style="margin-bottom: 12rpx;">
          设备号：{{ device_code }}
        </div>

        <u-form-item label="科室" borderBottom ref="item1" @click="show = true">
          <uni-data-select v-model="department_id" :localdata="departments" @change="change"></uni-data-select>
        </u-form-item>


        <u-form-item label="位置" borderBottom ref="item1">
          <u-input v-model="location" disabledColor="#ffffff" border="none"></u-input>
        </u-form-item>

        <u-button type="primary" @click="handleSubmit">保存入库</u-button>
      </u-form>
    </div>
  </view>
</template>

<script>
import { getDepartment, warehouseOperation } from "@/api/index";
import { getEquipmentInfo } from "../../../api";

// 锁类型：bluetooth_lock-蓝牙锁，concentric_lock-同心锁，chain_lock-链条锁，one_to_five_lock-一拖五锁，chair-椅子，cabinet-柜子，card_slot-卡槽，wheelchair-轮椅
export default {
  data() {
    return {
      model: {
        name: " ",
        sex: "",
      },
      // 科室列表
      departments: [],
      show: false,
      department_id: 0,

      // 锁的类型
      type: "",
      typeOptions: [
        {
          value: "bluetooth_lock",
          text: "蓝牙锁",
        },
        {
          value: "concentric_lock",
          text: "同心锁",
        },
        {
          value: "chain_lock",
          text: "链条锁",
        },
        {
          value: "one_to_five_lock",
          text: "一拖五锁",
        },
        {
          value: "chair",
          text: "椅子",
        },
        {
          value: "cabinet",
          text: "柜子",
        },
        {
          value: "card_slot",
          text: "卡槽",
        },
        {
          value: "wheelchair",
          text: "轮椅",
        },
      ],

      //
      building_number: "",
      floor: "",

      // 床號
      bed_number: "",
      // 具体位置
      location: "",

      // 设备号
      device_code: "",
    };
  },

  /**
   * 生命周期函数--监听页面加载
   */
  async onLoad(options) {
    const _res = await getDepartment({
      hospital_id: uni.getStorageSync("curHospital"),
    });

    _res?.forEach((_r) => {
      this.departments.push({
        value: _r.id,
        text: `${_r.deptname} (${_r.build}栋${_r.floor}楼)`,
        data: _r,
      });
    });
  },

  methods: {
    // 扫码
    handleScanRental() {

      const app = this;
      uni.scanCode({
        success: async (res) => {
          uni.showToast({ title: "扫码成功" });
          console.log(res);

          // 结构res.result https://www.fa.com/kpl/B6undbDGinql7bfE66i3by7Beblsprb41753596161
          // 获取kpl/后面的字符串
          let code = res.result.split("kpl/")[1];

          console.log('code', code);


          try {
            const _into = await getEquipmentInfo({
              id: code
            })
            console.log('_into', _into);

            if (_into.hospitals_id !== uni.getStorageSync("curHospital")) {
              return uni.showToast({ title: "设备不属于当前医院", icon: "none" });
            }

            app.department_id = _into.departments_id;
            app.location = _into.position;
            app.device_code = code;


          } catch (error) {

          }
        },
      });
    },
    change(value) {
      const _info = this.departments.find((_d) => _d.value == value);

      this.building_number = _info.data.build;
      this.floor = _info.data.floor;
    },
    // 提交表单
    async handleSubmit() {
      if (!this.department_id) {
        return uni.showToast({
          title: "请选择科室",
          icon: "none",
        });
      }

      if (!this.device_code) {
        return uni.showToast({
          title: "请先扫码设备二维码获取设备信息",
          icon: "none",
        });
      }

      if (!this.location) {
        return uni.showToast({
          title: "请填写设备位置",
          icon: "none",
        });
      }
      try {
        await warehouseOperation({
          // lock_type: this.type,
          department_id: this.department_id,
          // building_number: this.building_number,
          // floor: this.floor,
          location: this.location,
          device_code: this.device_code,
          // remark: "",
        });

        uni.showToast({
          title: "入库成功",
        });

        // 返回上一页
        uni.navigateBack({
          delta: 1,
        });
      } catch (error) { }
    },
  },
};
</script>

<style lang="scss" scoped>
.container {
  min-height: 100vh;
  background: #ebebeb;
  padding: 18rpx;
  padding-top: 30rpx;
}

.saoma_btn {
  width: 624rpx;
  height: 98rpx;
  line-height: 50rpx;
  border-radius: 42rpx;
  background: linear-gradient(92.01deg,
      rgba(87, 215, 130, 1) 38.63%,
      rgba(29, 221, 94, 1) 80.23%);
  color: rgba(255, 255, 255, 1);
  font-size: 36rpx;
  text-align: center;
  box-shadow: 0rpx 4rpx 12rpx 0rpx rgba(87, 215, 130, 1);
  font-family: PingFangSC-regular;

  margin: 0 auto;

  display: flex;
  align-items: center;
  justify-content: center;
}

.contont {
  border-radius: 24rpx;
  background-color: rgba(255, 255, 255, 1);
  width: 100%;
  padding: 30rpx;

  margin-top: 30rpx;
}
</style>
